﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace CSKB
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            CSKB cskb;
            if (args.Count() > 0)
            {                
                cskb = new CSKB(false);
            //    MessageBox.Show(System.Environment.CommandLine);
                InitCskbForm(args[0], cskb);
            }
            else
            {
                cskb = new CSKB(true);
            }
            Application.Run(cskb);
        }

        static void InitCskbForm(string filepath, CSKB cskbForm)
        {
            
                int pid;
                bool bAutoStart = false;
                cskbForm.EkbFilePath = filepath;    

                using (StreamReader sr = new StreamReader(filepath))
                {
                    string line = sr.ReadLine();
                    // MessageBox.Show(line);
                    string[] words = line.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                    foreach (string str in words)
                    {
                       // MessageBox.Show(str);
                        if (str.StartsWith("PID:"))
                        {
                            string v = str.Substring(4);
                            int.TryParse(v, out pid);
                            cskbForm.TargetPid = pid;
                        }
                        else if (str.StartsWith("Calc:"))
                        {
                            string v = str.Substring(5);
                            if (v == "1")
                            {
                                cskbForm.CkbCalc.CheckState = CheckState.Checked;
                            }
                            else
                            {
                                cskbForm.CkbCalc.CheckState = CheckState.Unchecked;
                            }
                        }
                        else if (str.StartsWith("AutoStart:"))
                        {
                            string v = str.Substring(10);
                            bAutoStart = (v == "1");
                            cskbForm.RunByUser = !bAutoStart;
                        }
                        else if (str.StartsWith("Note:"))
                        {
                            string v = str.Substring(5);
                            if (v == "1")
                                cskbForm.CkbNote.CheckState = CheckState.Checked;
                            else
                                cskbForm.CkbNote.CheckState = CheckState.Unchecked;
                        }
                        else if (str.StartsWith("HTML:"))
                        {
                            string v = str.Substring(5);
                            if (v == "1")
                                cskbForm.CkbHtml.CheckState = CheckState.Checked;
                            else
                                cskbForm.CkbHtml.CheckState = CheckState.Unchecked;
                        }
                    }
                }
            }         
        
    }
}
